<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      height: 500%;
      user-select: none;
    }

    .dialogMark {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 999;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.3);
    }

    .dialogBox {
      position: fixed;
      z-index: 1000;
      width: 200px;
      height: 300px;
      background-color: #fff;
      overflow: hidden;
    }

    .dialogBox h3.dialogTitle {
      position: relative;
      height: 35px;
      line-height: 35px;
      padding: 0 10px;
      background-color: #ddd;
      border-bottom: 1px solid #aaa;
      cursor: move;
    }

    .dialogBox h3.dialogTitle i {
      position: absolute;
      right: 10px;
      top: 50%;
      transform: translate(0, -50%);
      width: 20px;
      height: 20px;
      text-align: center;
      line-height: 20px;
      font-style: normal;
      font-weight: normal;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      font-size: 16px;
      border-radius: 50%;
      background-color: #aaa;
      color: #fff;
      cursor: pointer;
    }
  </style>
</head>

<body>
  <div class="dialogMark"></div>
  <div class="dialogBox">
    <h3 class="dialogTitle">Hello World<i class="dialogClose">X</i></h3>
    <div class="con">

    </div>
  </div>

  <script>
    window.onload = function () {
      let dialogMark = document.querySelector('.dialogMark');
      let dialogBox = document.querySelector('.dialogBox');
      let dialogTitle = document.querySelector('.dialogTitle');
      let dialogClose = document.querySelector('.dialogClose');

      // 屏幕高度宽度
      let winW = document.documentElement.clientWidth;
      let winH = document.documentElement.clientHeight;
      // 盒子的高度宽度
      let boxW = dialogBox.offsetWidth;
      let boxH = dialogBox.offsetHeight;

      // 设置盒子居中
      dialogBox.style.top = (winH - boxH) / 2 + 'px'
      dialogBox.style.left = (winW - boxW) / 2 + 'px'

      // 关闭
      dialogClose.onclick = function () {
        dialogMark.style.display = 'none'
        dialogBox.style.display = 'none'
      }

      let dragMove = function (ev) {
        let startX = parseFloat(this.dataset.startX)
        let startY = parseFloat(this.dataset.startY)
        let startL = parseFloat(this.dataset.startL)
        let startT = parseFloat(this.dataset.startT)
        let curL = ev.clientX - startX + startL;
        let curT = ev.clientY - startY + startT;

        // 边界判断
        let minL = 0;
        let minT = 0;
        let maxL = winW - boxW;
        let maxT = winH - boxH;
        curT = curT < minT ? minT : (curT > maxT ? maxT : curT);
        curL = curL < minL ? minL : (curL > maxL ? maxL : curL);

        dialogBox.style.top = curT + 'px';
        dialogBox.style.left = curL + 'px';
      }

      let dragEnd = function () {
        document.removeEventListener('mousemove', this.DRAG_MOVE)
        document.removeEventListener('mouseup', this.DRAG_END)
        this.DRAG_MOVE = null;
        this.DRAG_END = null;
      }

      let dragStart = function (ev) {
        this.setAttribute('data-start-X', ev.clientX);
        this.setAttribute('data-start-Y', ev.clientY);
        this.setAttribute('data-start-L', dialogBox.offsetLeft);
        this.setAttribute('data-start-T', dialogBox.offsetTop);

        // 注意 this 指向
        this.DRAG_MOVE = dragMove.bind(this)
        this.DRAG_END = dragEnd.bind(this)

        document.addEventListener('mousemove', this.DRAG_MOVE)
        document.addEventListener('mouseup', this.DRAG_END)
      }

      dialogTitle.addEventListener('mousedown', dragStart)

      window.onresize = function () {
        // 屏幕高度宽度
        let resozeWinW = document.documentElement.clientWidth;
        let resozeWinH = document.documentElement.clientHeight;
        // 设置盒子居中
        dialogBox.style.top = (resozeWinH - boxH) / 2 + 'px';
        dialogBox.style.left = (resozeWinW - boxW) / 2 + 'px';
      }

    }
  </script>
</body>

</html>